#!/usr/bin/env python

import pypvm
import sys
from signal import signal, SIGTERM
from time import sleep

# ============================ sig_h () =======================================
def sig_h (signum, traceback):
    print "SLAVE: got signal", signum
try: 

    signal (SIGTERM, sig_h)

# ============================ test parent () =================================
    parent = pypvm.parent ()

# ============================ test probe () ==================================
    print "SLAVE: testing probe..."
    bufid = pypvm.probe (parent, 1)
    print "SLAVE: probe returned", bufid

# ============================ test bufinfo () ================================
    print "SLAVE: testing bufinfo..."
    if bufid:
        print "SLAVE: bufinfo returned", pypvm.bufinfo (bufid)
    else:
        print "SLAVE: no incoming messages"

# ============================ test recv () ===================================
    print "SLAVE: receiving data from tid %s..." % parent
    pypvm.recv (parent, 1)

# ============================ test up () =====================================
    print "SLAVE: unpacking data..."
    data = pypvm.upk ()
    print "SLAVE: data received = %s" % data


# ============================ sleep ==========================================
    print "SLAVE: sleeping so I don't miss the signal"
    try:
        sleep (2)
    except IOError:
        # interrupted system call
        pass

    pypvm.initsend(pypvm.data['default'])
    print "SLAVE: sending data to master..."
    my_data = ['Pero vo so loco viteh', 'Jojojo']
    pypvm.pk(my_data)
    pypvm.send(parent, 2)
# ============================ test exit () ===================================
    print "SLAVE: exiting pvm..."
    pypvm.exit ()

except:
    print "A pypvm error occured!"
    print sys.exc_info()
